package controller;

import aop.LogData;
import aop.LogType;
import com.github.pagehelper.PageInfo;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import service.CustomerService;
import service.LinkmanService;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private LinkmanService linkmanService;
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/toadd")
    public String toadd(ModelMap modelMap){
        //查询所有的角色信息
        List<Customer> customers=customerService.selectAll();
        //将角色信息返回到前台页面
        modelMap.addAttribute("customers",customers);
        return "add.jsp";
    }

    @LogData(logType = LogType.INSERT,description = "添加客户")
    @ResponseBody
    @RequestMapping("/add")
    public MyResult add(Customer customer,HttpSession session){
        System.out.println("进入控制层方法");
        Users users = (Users) session.getAttribute("users");
        if(users!=null){
            customer.setCreater(users.getId());
        }else{
            customer.setCreater(-1);
        }
        int flag = customerService.insert(customer);
        return new MyResult<>(0);
    }
    @RequestMapping("/toupdate")
    public String toupdate(Integer id,ModelMap modelMap){
        Customer customer = customerService.selectById(id);
        modelMap.addAttribute("customer",customer);
        return "update.jsp";
    }

    @LogData(logType = LogType.UPDATE,description = "修改客户信息")
    @ResponseBody
    @RequestMapping("/update")
    public MyResult update(Customer customer){
        boolean i=customerService.update(customer);
        return new MyResult(0);
    }
    @RequestMapping("/customerDetails")
    public String customerDetail(ModelMap modelMap){
        List<Customer> customers = customerService.selectAll();
        modelMap.addAttribute("customers",customers);
        return "customerDetail.jsp";
    }

    @RequestMapping("/tosearch")
    public String tosearch(ModelMap modelMap){
        List<Customer> customers = customerService.selectAll();
        modelMap.addAttribute("customers",customers);
        return "search.jsp";
    }

    @ResponseBody
    @RequestMapping("/customers")
    public MyResult<List<Customer>> customer(LayRequest layRequest, ModelMap modelMap){
        //执行分页查询
        PageInfo<Customer> customerPageInfo=customerService.selectByPage(layRequest);
        //将分页查询结果传递给页面
        return new MyResult<>(0,customerPageInfo.getTotal(),customerPageInfo.getList());
    }
    @ResponseBody
    @PostMapping("/batchDelete")
    public MyResult batchDelete(Integer [] ids){
        customerService.batchDelete(ids);
        return new MyResult(0);
    }

    @RequestMapping("/transfers")
    public String transfer(ModelMap modelMap){
        List<Customer> customers = customerService.selectAll();
        modelMap.addAttribute("customers",customers);
        return "transfer.jsp";
    }

    @ResponseBody//表示返回json数据
    @RequestMapping(value = "/batchdelete",method = RequestMethod.GET)//等同于@GetMapping("/delete")
    public MyResult batchdelete(Integer id){
        boolean flag = customerService.delete(id);
        return flag?new MyResult(200,"删除成功！")
                :new MyResult(200,"删除失败");
    }

    //@SessionAttribute("admin")
    //MultipartFile 对象来接受上传文件，参数名与input中name名一致
    @RequestMapping("/headpic")
    public String headpic(MultipartFile headpic, @RequestParam(required = false) Customer customer, Integer id) throws IOException {
        String filename = headpic.getOriginalFilename();
        System.out.println("上传文件名："+filename);
        File file=new File("E:/headpic/"+filename);
        if (!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        headpic.transferTo(file);
        return "redirect:customer";
    }

    @RequestMapping("/tolist")
    public String tolist(ModelMap modelMap){
        List<Customer> customerList = customerService.selectAll();
        modelMap.addAttribute("customerList",customerList);
        return "/linkman/track.jsp";
    }

}
